package com.wechar_rpa.service;

import com.wechar_rpa.model.User;
import com.wechar_rpa.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public Optional<User> getUserByUsername(String username) {
        return userRepository.findByUsername(username);
    }

    public User saveUser(User user) {
        return userRepository.save(user);
    }

    public Boolean isUserWhitelisted(String username) {
        Optional<User> user = userRepository.findByUsername(username);
        return user.map(User::getIsWhitelisted).orElse(false);
    }

    public List<User> getWhitelistedUsers() {
        return userRepository.findByIsWhitelistedTrue();
    }

    public List<User> findAll() {
        return userRepository.findAll();
    }
}